package com.gqz.equip.app.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.app.entity.EnStockTaskOperatePtEntity;
import com.gqz.equip.app.mapper.EnStockTaskOperateMapper;
import com.gqz.equip.app.mapstruct.EnStockTaskOperateMapstruct;
import com.gqz.equip.app.service.IEnStockTaskOperateService;
import com.gqz.equip.vo.EnStockTaskOperateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class EnStockTaskOperateServiceImpl extends ServiceImpl<EnStockTaskOperateMapper, EnStockTaskOperatePtEntity> implements IEnStockTaskOperateService {

    @Autowired
    private EnStockTaskOperateMapstruct mapstruct;

    @Override
    public List<EnStockTaskOperateVo> getById(String id) {
        MPJLambdaWrapper<EnStockTaskOperatePtEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnStockTaskOperatePtEntity::getId, id);
        queryWrapper.selectAll(EnStockTaskOperatePtEntity.class);
        List<EnStockTaskOperatePtEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnStockTaskOperateVo> page(RequestForm<EnStockTaskOperateVo> requestForm) {
        EnStockTaskOperatePtEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnStockTaskOperatePtEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(EnStockTaskOperatePtEntity.class);
        Page<EnStockTaskOperatePtEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<EnStockTaskOperatePtEntity> iPage = baseMapper.selectJoinPage(page, EnStockTaskOperatePtEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnStockTaskOperateVo> requestForm) {
        EnStockTaskOperatePtEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnStockTaskOperatePtEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.select(EnStockTaskOperatePtEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnStockTaskOperateVo> vos) {
        List<EnStockTaskOperatePtEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnStockTaskOperatePtEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnStockTaskOperateVo> forms) {
        List<EnStockTaskOperatePtEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnStockTaskOperatePtEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }


}
